﻿//-----------------------------------------------------------------------
// <copyright file="LogEntryEntity.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.Diagnostics.Model
{
    using System;

    using System.Collections.Generic;

    using System.Xml.Linq;
    
    using CodePlex.Diagnostics.Contracts;

    /// <summary>
    /// Represents an entry within the log.
    /// </summary>
    public partial class LogEntryEntity
    {
        #region public LogEntryEntity()
        
        /// <summary>
        /// Initializes a new instance of the <see cref="T:LogEntryEntity"/> class.
        /// </summary>
        public LogEntryEntity()
        {
        } 

        #endregion

        #region public LogEntryEntity(LogEntryDataContract contract, Dictionary<string, string> dictionary, XElement element, IIdentityDataContract identity)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:LogEntryEntity"/> class.
        /// </summary>
        /// <param name="contract">An instance of the <see cref="T:LogEntryDataContract"/> class to be used to construct an instance of the <see cref="T:LogEntryEntity"/> class.</param>
        /// <param name="dictionary">An optional <see cref="T:System.Collections.Generic.Dictionary`2"/> containing name value pairs associated with the log entry.</param>
        /// <param name="element">An optional <see cref="T:System.Linq.Xml.XElement"/> containing Xml associated with the log entry.</param>
        /// <param name="identity">An instance of the <see cref="T:IIdentityDataContract"/> class to be used to construct an instance of the <see cref="T:LogEntryEntity"/> class.</param>
        public LogEntryEntity(LogEntryDataContract contract, Dictionary<string, string> dictionary, XElement element, IIdentityDataContract identity)
        {
            if (contract.AppDomain != null)
            {
                this.AppDomainEntity = new AppDomainEntity(contract.AppDomain);
            }

            if (contract.Assembly != null)
            {
                this.AssemblyEntity = new AssemblyEntity(contract.Assembly);
            }

            this.LogEntry = contract.Entry;

            if (contract.Machine != null)
            {
                this.MachineEntity = new MachineEntity(contract.Machine);
            }

            this.IIdentityEntity = new IIdentityEntity(identity);

            if (contract.Process != null)
            {
                this.ProcessEntity = new ProcessEntity(contract.Process);
            }

            this.PublicationId = contract.PublicationId;

            if (contract.Thread != null)
            {
                this.ThreadEntity = new ThreadEntity(contract.Thread);
            }

            this.XElement = element != null ? element.ToString(SaveOptions.None) : null;

            this.SerializedDictionaryXml = Serializer<Dictionary<string, string>>.Serialize(dictionary);
        }

        #endregion
    }
}
